﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestSteer : MonoBehaviour {
    List<Vehicle> vehicle;

    public float gapCalF = 0.2f;
    public float lastCalF = 0;

    public float gapUpPos = 0.2f;
    public float lastUpPos = 0;

    public float gapFreshRadar = 0.2f;
    public float lastFreshRadar = 0;

    public string steerName = "";
    // Use this for initialization
    void Start () {
	}

    // Update is called once per frame
    void Update() {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            Vehicle[] objs = GetComponentsInChildren<Vehicle>();
            vehicle = new List<Vehicle>(objs);
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            for (int i = 0; i < vehicle.Count; i++)
            {
                vehicle[i].GetSteers();
            }
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            if(vehicle != null)
            {
                for (int i = 0; i < vehicle.Count; i++)
                {
                    SteerForPoint forPoint = vehicle[i].gameObject.GetComponent<SteerForPoint>();
                    if (forPoint != null)
                    {
                        forPoint.TagPoint = new Vector3(0, vehicle[i].transform.position.y, vehicle[i].transform.position.z);
                        forPoint.OnArrival = (SteerBase steer) =>
                        {
                            Debug.LogFormat("{0}  ",steer.gameObject.name);
                            SteerSeparation separation = steer.gameObject.GetComponent<SteerSeparation>();
                            if(separation != null)
                            {
                                separation.enabled = false;
                            }
                        };
                    }
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            if(vehicle != null)
            {
                for (int i = 0; i < vehicle.Count; i++)
                {
                    SteerBase script = vehicle[i].gameObject.GetComponent(steerName) as SteerBase;
                    if(script != null)
                    {
                        script.enabled = !script.enabled;
                        if (steerName.Equals("SteerForPoint") && script.enabled)
                        {
                            Animation animation = vehicle[i].gameObject.GetComponentInChildren<Animation>();
                            if(animation != null)
                            {
                                animation.wrapMode = WrapMode.Loop;
                                animation.Play("batman@run");
                            }
                        }
                    }
                }
            }
        }

        if (Time.time + gapCalF >= lastCalF)
        {
            lastCalF = Time.time;
            if (vehicle != null)
            {
                for (int i = 0; i < vehicle.Count; i++)
                {
                    vehicle[i].CalculateForces();
                }
            }
        }
        if (Time.time + gapUpPos>= lastUpPos)
        {
            lastUpPos = Time.time;
            if (vehicle != null)
            {
                for (int i = 0; i < vehicle.Count; i++)
                {
                    vehicle[i].UpdatePos(Time.deltaTime);
                }
            }
        }
        if (Time.time + gapFreshRadar >= lastFreshRadar)
        {
            lastFreshRadar = Time.time;
            if (vehicle != null)
            {
                for (int i = 0; i < vehicle.Count; i++)
                {
                    vehicle[i].FreshRadar();
                }
            }
        }
    }
}
